package com.mastercard.labs

import java.text.DateFormat;
import java.text.SimpleDateFormat;

class DateUtil {
	public static String DATE_FORMAT = "dd/MM/yyyy";
	public static String DATE_TIME_FORMAT = "dd/MM/yyyy HH:mm:ss";
	
	def static formatDate(date, format) {
		String str = null
		if(date){
			if(!format){
				format = DATE_FORMAT
			}
			DateFormat df = DateFormat.getDateInstance()
			SimpleDateFormat sdf = (SimpleDateFormat) df
			sdf.setLenient(false)
			sdf.applyPattern(format)
			str = sdf.format(date)
		}
		return str
	}
	
	def static formatDate(date) {
		return formatDate(date, this.DATE_FORMAT)
	}
	
	def static parseDate(date, format) {
		Date parsedDate = null
		DateFormat df = DateFormat.getDateInstance()
		SimpleDateFormat sdf = (SimpleDateFormat) df
		sdf.setLenient(false) // added for strict checking of date object
		sdf.applyPattern(format)
		
		if (date) {
			parsedDate = sdf.parse(date)
		}
		return parsedDate
	}

	def static addMonths(date, amount) {
		return add(date, Calendar.MONTH, amount)
	}
	
	def static add(date, calendarField, amount) {
		Date dt = null
		if (date) {
			Calendar c = Calendar.getInstance()
			c.setTime(date)
			c.add(calendarField, amount)
			dt = c.getTime()
		}
		
		return dt
	}
}
